package com.cyandpos.pad.utils.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.ToastUtils;

/**
 * 输入框弹窗
 */
public class InputNumberDialogCommon extends AlertDialog {
    private static  InputNumberDialogCommon inputNumberDialogCommon;
    private Context context;
    private View view;
    private TextView tv_title;
    private RelativeLayout rl_close;
    private TextView tv_content;
    private EditText ed_cashbox_pw;
    private Button btn_opencashbox;
    private SelfNumKeyView ed_cashbox_keyboard;
    private InputDialogOnSubmitClickListener inputDialogOnSubmitClickListener;
    protected InputNumberDialogCommon(Context context, final InputDialogOnSubmitClickListener inputDialogOnSubmitClickListener) {
        super(context);
        this.context=context;
        this.inputDialogOnSubmitClickListener=inputDialogOnSubmitClickListener;
        view = LayoutInflater.from(context).inflate(R.layout.dialog_input_number_common,null);
        rl_close=view.findViewById(R.id.rl_close);
        tv_title=view.findViewById(R.id.tv_title);
        tv_content=view.findViewById(R.id.tv_content);
        ed_cashbox_pw=view.findViewById(R.id.ed_cashbox_pw);
        btn_opencashbox=view.findViewById(R.id.btn_opencashbox);
        ed_cashbox_keyboard = view.findViewById(R.id.ed_cashbox_keyboard);
        ed_cashbox_keyboard.setIsCanInputPoint(true);
        ed_cashbox_keyboard.setEnterReceptor(ed_cashbox_pw);
        ed_cashbox_keyboard.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
                if(num!=null&&!"".equals(num)) {
                    if (inputDialogOnSubmitClickListener != null) {
                        inputDialogOnSubmitClickListener.InputDialogOnSubmitClick(num);
                        inputNumberDialogCommon.dismiss();
                        inputNumberDialogCommon=null;
                    }
                }else{
                    ToastUtils.show("输入内容不能为空!");
                }
            }
        });

        btn_opencashbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = ed_cashbox_pw.getText()+"";
                if(content!=null&&!"".equals(content)) {
                    if (inputDialogOnSubmitClickListener != null) {
                        inputDialogOnSubmitClickListener.InputDialogOnSubmitClick(content);
                        inputNumberDialogCommon.dismiss();
                        inputNumberDialogCommon=null;
                    }
                }else{
                    ToastUtils.show("输入内容不能为空!");
                }
            }
        });
    }

    public static  InputNumberDialogCommon getInputNumberDialogCommon(Context context, InputDialogOnSubmitClickListener inputDialogOnSubmitClickListener){
        if(inputNumberDialogCommon==null){
            inputNumberDialogCommon=new InputNumberDialogCommon(context,inputDialogOnSubmitClickListener);
        }
        return inputNumberDialogCommon;
    }

    /**
     * 是否允许输入小数点
     * @param
     */
    public void setIsCanInputPoint(boolean isCanInputPoint){
        ed_cashbox_keyboard.setIsCanInputPoint(isCanInputPoint);
    }
    /**
     * 是输入前是否清空
     * @param
     */
    public void setIsCanInputClear(boolean isCanInputClear){
        ed_cashbox_keyboard.setIsClearEt(isCanInputClear);
    }
    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        tv_title.setText(title);
    }

    /**
     * 设置描述内容
     * @param content
     */
    public void setContent(String content){
        tv_content.setText(content);
    }

    /**
     * 设置输入框描述内容
     * @param hint
     */
    public void setEtHint(String hint){
        ed_cashbox_pw.setHint(hint);
    }

    /**
     * 设置输入框内容
     * @param text
     */
    public void setEtText(String text){
        ed_cashbox_pw.setText(text);
        ed_cashbox_pw.setSelection(ed_cashbox_pw.getText().length());
    }
    public interface InputDialogOnSubmitClickListener{
        void InputDialogOnSubmitClick(String content);
    }

    /**
     * 显示弹窗
     *
     * @param isClose
     */
    public  void showDialog(boolean isClose){
        inputNumberDialogCommon.setCancelable(isClose);
        rl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputNumberDialogCommon.dismiss();
                inputNumberDialogCommon=null;
            }
        });

        inputNumberDialogCommon.setView(view,0,0,0,0);
        inputNumberDialogCommon.show();
        inputNumberDialogCommon.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg_common);
    }
}
